//
//  ZJHBaseRequest.m
//  ZiJinHuaDemo
//
//  Created by 李宗帅 on 2018/4/2.
//  Copyright © 2018年 ZiJinHuaLive.com. All rights reserved.
//

#import "ZJHBaseRequest.h"
#import "LTXRequestLogger.h"
@interface ZJHBaseRequest ()

@property (nonatomic, copy) NSString *deviceType;

@property (nonatomic, copy) NSString *deviceId;

@property (nonatomic, copy) NSString *versionCode;

@property (nonatomic, copy) NSString *channel;//渠道ID

@end

@implementation ZJHBaseRequest

- (instancetype)init {
    self = [super init];
    if (!self) return nil;
    
    [self commonSetup];
    
    return self;
}

#pragma mark __PRI__
- (void)commonSetup {
    _deviceType = @"ios";
    _deviceId = [FCUUID uuidForDevice];
    _versionCode = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
    _channel = [self getAppChannel];
}

- (NSString*)getAppChannel {
    return  @"appStore";
}


- (NSString *)requestNewBaseUrl
{
    return @"";
}

- (YTKRequestMethod)requestMethod
{
    return YTKRequestMethodPOST;
}


- (void)startWithCompletionBlockWithSuccess:(YTKRequestCompletionBlock)success failure:(YTKRequestCompletionBlock)failure
{
    [LTXRequestLogger logRequestWithYCLRequest:self YTKRequest:nil error:nil];
    [super startWithCompletionBlockWithSuccess:success failure:failure];
}

- (NSString *)requestUrl
{
    NSString* url = [self requestNewBaseUrl];
    NSString *baseUrl = @"";
    NSRange range = [url rangeOfString:@"?"];
    if (range.location != NSNotFound) {
        baseUrl = [NSString stringWithFormat:@"%@&deviceType=%@&deviceId=%@&versionCode=%@&channel=%@",[self requestNewBaseUrl],_deviceType,_deviceId,_versionCode,_channel];
        
    }
    else {
        baseUrl = [NSString stringWithFormat:@"%@?deviceType=%@&deviceId=%@&versionCode=%@&channel=%@",[self requestNewBaseUrl],_deviceType,_deviceId,_versionCode,_channel];
        
    }
    return baseUrl;
}


- (void)setCompletionBlockWithSuccess:(YTKRequestCompletionBlock)success failure:(YTKRequestCompletionBlock)failure
{
    @weakify(self);
    void (^suc)(YTKBaseRequest *) = ^(YTKBaseRequest *request) {
        @strongify(self);
        [LTXRequestLogger logResponseWithYCLRequest:self YTKRequest:request error:nil];
        success(request);
    };
    
    void (^fail)(YTKBaseRequest *) = ^(YTKBaseRequest *request) {
        @strongify(self);
        [LTXRequestLogger logResponseWithYCLRequest:self YTKRequest:request error:request.error];
        failure(request);
    };
    
    [super setCompletionBlockWithSuccess:suc failure:fail];
}


@end
